
<script>
import { getCardListAPI, deleteCardAPI } from '@/api/car'
export default {
  name: 'Card',
  data() {
    return {
      params: {
        page: 1, // 第几页
        pageSize: 2, // 每页页容量
        carNumber: '', // 车牌号
        personName: '', // 车主姓名
        cardStatus: null // '0':可用 '1':已过期,现在要获取全部数据，设置为null，这样就不会传这个数据在请求后端数据时。
      },
      list: [],
      total: 0,
      options: [{ text: '全部', value: null }, { text: '可用', value: 0 }, { text: '已过期', value: 1 }],
      selectionData: []
    }
  },
  created() {
    console.log(this.$store.state.user.token)
    this.getCardList()
  },
  methods: {
    batchDelCard() {
      if (this.selectionData <= 0) {
        this.$message('请选择您要删除的数据')
      } else {
        this.$confirm('您是否要永久删除这些数据？', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          const ids = this.selectionData.map((item) => { return item.id })
          await deleteCardAPI(ids.join(','))
          this.getCardList()
          this.$message.success('批量删除数据成功')
        }).catch(() => {})
      }
    },
    handleSelectionChange(val) {
      // console.log(val)
      this.selectionData = val
    },
    deleteCard(id) {
      // console.log(id)
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteCardAPI(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 如果删除的是最后一条数据，获取列表时，应该让page-1
        if (this.list.length === 1 && this.params.page > 1) {
          this.params.page--
        }
        this.getCardList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
        this.getCardList()
      })
    },
    editCard(id) {
      this.$router.push({
        path: '/addCard',
        query: {
          id
        }
      })
    },
    search() {
      this.params.page = 1
      this.getCardList()
    },
    indexMethod(index) {
      return (index + 1) + (this.params.pageSize) * (this.params.page - 1)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.params.pageSize = val
      this.params.page = 1
      this.getCardList()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getCardList()
    },
    formatterValue(row, column, cellValue, index) {
      // console.log(row, column, cellValue, index)
      const MAP = {
        '0': '可用',
        '1': '已过期'
      }
      return MAP[cellValue]
    },
    async getCardList() {
      console.log('请求参数:', this.params) // 添加这行，查看参数是否正确
      const res = await getCardListAPI(this.params)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
      console.log('当前列表数据:', this.list)
      console.log('总条数:', this.total)
    }
  }

}
</script>

<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.text"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="search()">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/addCard')">添加月卡</el-button>
      <el-button @click="batchDelCard">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55"
        />
        />
        <el-table-column type="index" label="序号" :index="indexMethod" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="cardStatus" :formatter="formatterValue" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="editCard(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="deleteCard(scope.row.id)">删除</el-button>
          </template>
        </el-table-column></el-table>
    </div>
    <div class="page-container">
      <!-- current-change: -->
      <!-- current-page：表示当前页码 -->
      <!-- page-sizes：可选择每页的页容量 -->
      <!-- page-size:当前页的页容量 -->
      <!-- layout：表示分页的样式 -->
      <!-- total：表示一共多少条数据 -->
      <!-- @size-change：页容量发生变化时会触发这个函数 -->
      <!-- @current-change：页码发生变化时会触发这个函数 -->
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 4, 10, 15]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog
      title="添加楼宇"
      width="580px"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
